package com.yt.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SecurityServer extends Thread {

	private ServerSocket serverSocket;
	private BufferedReader reader;
	private BufferedWriter writer;

	public SecurityServer() {
		try {
			serverSocket = new ServerSocket(843);
			System.out.println("安全服务器建立完毕");
			this.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		Socket socket = null;
		try {
			while (true) {
				String xml = "<cross-domain-policy>"
						+ "<site-control permitted-cross-domain-policies=\"all\"/>"
						+ "<allow-access-from domain=\"*\" to-ports=\"*\"/>"
						+ "</cross-domain-policy>\0";
				socket = serverSocket.accept();
				reader = new BufferedReader(new InputStreamReader(socket
						.getInputStream(), "UTF-8"));
				writer = new BufferedWriter(new OutputStreamWriter(socket
						.getOutputStream(), "UTF-8"));
				char[] buf = new char[22];
				reader.read(buf);
				String s = new String(buf);
				if (s.equals("<policy-file-request/>")) {
					writer.write(xml);
					writer.flush();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				writer.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
